package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.NetworkResponse;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.MyTeamAdapter;
import com.lanou.sinasports.beans.GsonMyTeam;
import com.lanou.sinasports.beans.GsonMyTeamPre;
import com.lanou.sinasports.beans.MyTeam;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.VolleySingleton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class MyTeamFragment extends Fragment {
    private Context context;
    private ListView listView;
    private MyTeamAdapter adapter;
    private List<MyTeam> data;
    private VolleySingleton queue;
    private String url = "http://platform.sina.com.cn/sports_client/attentions?app_key=2586208540&_version__" +
            "=3.2.0.14&__os__=android&format=json&token=866500020074540&gdid=SPNS-YH-3568C7804BpPEzI0aznnUlFrifMNnMeI&type=&appid=102";
    StringBuffer sb = new StringBuffer();
    String id="";
    private List<String> ids;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_myteam, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_my_team);

    }

    private Handler handler;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        data = new ArrayList<>();

        queue = VolleySingleton.getInstance();

        ids = new ArrayList<>();

        GsonRequest<GsonMyTeamPre> request = new GsonRequest<GsonMyTeamPre>(url, GsonMyTeamPre.class, new Response.Listener<GsonMyTeamPre>() {
            @Override
            public void onResponse(GsonMyTeamPre gsonMyTeamPre) {

                int size = gsonMyTeamPre.getResult().getData().getTeam().size();
               // Log.d("MyTeamFragment", "size:" + size);
                for (int i = 0; i < size; i++) {

                    id = gsonMyTeamPre.getResult().getData().getTeam().get(i).getTeam_id();
                    ids.add(id);
                    Log.d("MyTeamFragment", "ids.size():" + ids.size());
                    if (i == (size - 1)) {
                        sb.append(id);
                    } else {
                        sb.append(id + ",");

                    }


                }
                Log.d("5555555555", "++++++=" + sb);
                String ur = "http://platform.sina.com.cn/sports_client/team_match?app_key=2586208540&_version__=3.2.0.14&__os__=android&team_id="
                        + sb + "&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";
                Log.d("00000000", ur);
                Message msg = handler.obtainMessage();
                msg.obj = ur;
                handler.sendMessage(msg);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


//        Log.d("MyTeamFragment", "888888888" + sb);
//
//        final String ur = "http://platform.sina.com.cn/sports_client/team_match?app_key=2586208540&_version__=3.2.0.14&__os__=android&team_id="
//                +sb+ "&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";
//
//        Log.d("-------6666666", ur);
//
//        Log.d("--------------", sb + "---------");



        queue._addRequest(request);


        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                final String ur = (String) msg.obj;

                StringRequest stringRequest = new StringRequest(ur, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("999999999", ur);
                        int size = ids.size();
                        for(int i = 0;i< ids.size(); i++) {
                          //  Log.d("MyTeamFragment", ids.get(i));
                        }
                        try {

                            JSONObject o = new JSONObject(response);
                            JSONObject objectOne = o.getJSONObject("result");
                            if (objectOne.has("data")) {
                                JSONObject objectTwo = objectOne.getJSONObject("data");


                                for (int i = 0; i < size; i++) {
                                    if (objectTwo.has(ids.get(i))) {
//                                JSONObject object=new JSONObject(ids.get(i));
                                        JSONObject object = objectTwo.getJSONObject(ids.get(i));
                                        String name = (String) object.get("Team1");
                                        String date = (String) object.get("date");
                                        String time = (String) object.get("time");
                                        String flag1 = (String) object.get("Flag1");
                                        String flag2 = (String) object.get("Flag2");

                                        MyTeam myTeam = new MyTeam();
                                        myTeam.setName(name);
                                        myTeam.setDate(date);
                                        myTeam.setTime(time);
                                        myTeam.setFlag1(flag1);
                                        myTeam.setFlag2(flag2);

                                        data.add(myTeam);
                                       // Log.d("MyTeamFragment", "data.size():" + data.size());
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        adapter = new MyTeamAdapter(context, data);
                        listView.setAdapter(adapter);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

                queue._addRequest(stringRequest);


                return false;
            }
        });




    }
}
